package com.kmproject.haifu.mvp.ui.widget;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kmproject.haifu.R;
import com.kmproject.haifu.mvp.model.entity.MessageEvent;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.weigan.loopview.LoopView;
import com.weigan.loopview.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.List;

/**
 * ================================================
 * 作    者：JayGoo
 * 版    本：1.1.0
 * 创建日期：2017/2/22
 * 描    述:
 * ================================================
 */
public class MultiSelectPopWindow1 {
    private RelativeLayout.LayoutParams layoutParams;
    private PopupWindow mPopupWindow;
    private TextView cancelBtn;
    private TextView confirmBtn;
    private TextView titleTV;
    private LoopView loopView;
    private LoopView loopView1;
    private LoopView loopView2;
    private OnConfirmClickListener mOnConfirmListener;
    private ArrayList<Integer> mIndexList;
    private Builder mBuilder;
    private RelativeLayout rootview1, rootview2, rootview3;


    public interface OnConfirmClickListener {
        void onClick(MessageEvent messageEvent);
    }

    static public class Builder {
        private Activity mActivity;
        private List<HousePayWayBean> choiceRoles = new ArrayList<>();
        List<String> firstNames = new ArrayList<>();
        List<String> secondNames = new ArrayList<>();
        List<String> thirdNames = new ArrayList<>();
        //        List<AllRolesBean.RolesBean>
        private String title;
        private String confirmText;
        private String cancelText;
        private boolean isOutsideTouchable;
        private View.OnClickListener mOnCancelListener;
        private OnConfirmClickListener mOnConfirmListener;
        private int mConfirmTextColor;
        private int mCancelTextColor;
        private int mTitleTextColor;
        private int defultIndex = 0;
        private boolean isSetData = false;
        private boolean ishx = false;//是不是户型，单独处理

        public Builder(Activity mActivity) {
            this.mActivity = mActivity;
        }

        public Builder setisSetData(boolean isSetData) {
            this.isSetData = isSetData;
            return this;
        }

        public Builder setishx(boolean ishx) {
            this.ishx = ishx;
            return this;
        }

        public Builder setFirstDataArray(List<String> firstNames) {
            this.firstNames = firstNames;
            return this;
        }

        public Builder setSecondDataArray(List<String> secondNames) {
            this.secondNames = secondNames;
            return this;
        }

        public Builder setThirdDataArray(List<String> thirdNames) {
            this.thirdNames = thirdNames;
            return this;
        }

        public Builder setDataArray(List<HousePayWayBean> list) {
            this.choiceRoles = list;
            return this;
        }


        /**
         * set title
         *
         * @param title
         * @return
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * set confirm button text
         *
         * @param str
         * @return
         */
        public Builder setConfirm(String str) {
            this.confirmText = str;
            return this;
        }

        /**
         * set cacel button text
         *
         * @param str
         * @return
         */
        public Builder setCancel(String str) {
            this.cancelText = str;
            return this;
        }

        /**
         * set title's text color
         *
         * @param color
         * @return
         */
        public Builder setTitleTextColor(int color) {
            this.mTitleTextColor = color;
            return this;
        }

        /**
         * 设置默认选中
         *
         * @param defultIndex
         * @return
         */
        public Builder setDefultSelect(int defultIndex) {
            this.defultIndex = defultIndex;
            return this;
        }

        /**
         * set confirm button's text color
         *
         * @param color
         * @return
         */
        public Builder setConfirmTextColor(int color) {
            this.mConfirmTextColor = color;
            return this;
        }

        /**
         * set cancel button's text color
         *
         * @param color
         * @return
         */
        public Builder setCancelTextColor(int color) {
            this.mCancelTextColor = color;
            return this;
        }

        /**
         * set if can touchable if your finger touch outside
         *
         * @param isOutsideTouchable
         * @return
         */
        public Builder setOutsideTouchable(boolean isOutsideTouchable) {
            this.isOutsideTouchable = isOutsideTouchable;
            return this;
        }

        public MultiSelectPopWindow1 build() {
            return new MultiSelectPopWindow1(this);
        }

        public Builder setConfirmListener(OnConfirmClickListener listener) {
            this.mOnConfirmListener = listener;
            return this;
        }

        public Builder setCancelListener(View.OnClickListener listener) {
            this.mOnCancelListener = listener;
            return this;
        }


    }

    private MultiSelectPopWindow1(final Builder builder) {
        mBuilder = builder;
        //init PopWindow
        View popview = View.inflate(builder.mActivity, R.layout.multi_select_list_popwindow1, null);
        mPopupWindow = new PopupWindow(popview, WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.WRAP_CONTENT);
        mPopupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
        mPopupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        mPopupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mPopupWindow.setFocusable(true);
        mPopupWindow.setTouchable(true);
        mPopupWindow.setOutsideTouchable(builder.isOutsideTouchable);

        //阴影
        initViews(mPopupWindow.getContentView());
        initListener();
        if (!mBuilder.firstNames.isEmpty()) {
            showloopView(getNames(null), mBuilder.defultIndex);
            showloopView1(getSecondNames(null), mBuilder.defultIndex);
            showloopView2(getThirdNames(null), mBuilder.defultIndex);
        }

        if (mBuilder.choiceRoles != null && !mBuilder.choiceRoles.isEmpty()) {
            showloopView(getNames(mBuilder.choiceRoles), mBuilder.defultIndex);
            List<HousePayWayBean> second = mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren();
            if (!second.isEmpty()) {
                showloopView1(getSecondNames(second), mBuilder.defultIndex);
            }
            if (!second.isEmpty()) {
                showloopView2(getThirdNames(second.get(loopView1.getSelectedItem()).getChildren()), mBuilder.defultIndex);
            }
        }


    }


    /**
     * 根据第二级角色名字获取第三级的角色名字
     *
     * @param choiceRoles
     * @param
     * @return
     */
    public List<String> getThirdNames(List<HousePayWayBean> choiceRoles) {
        if (!mBuilder.isSetData) {
            mBuilder.thirdNames.clear();
            if (choiceRoles != null && !choiceRoles.isEmpty()) {
                for (HousePayWayBean housePayWayBean : choiceRoles) {
                    mBuilder.thirdNames.add(housePayWayBean.getName());
                }
            }
        }
        return mBuilder.thirdNames;
    }

    /**
     * 根据第一级角色名字获取第二级的角色名字
     *
     * @param choiceRoles
     * @param
     * @return
     */
    public List<String> getSecondNames(List<HousePayWayBean> choiceRoles) {
        if (!mBuilder.isSetData) {
            mBuilder.secondNames.clear();
            if (choiceRoles != null && !choiceRoles.isEmpty()) {
                for (HousePayWayBean housePayWayBean : choiceRoles) {
                    mBuilder.secondNames.add(housePayWayBean.getName());

                }
            }
        }

        return mBuilder.secondNames;
    }

    /**
     * 得到角色名字
     *
     * @param choiceRoles
     * @return
     */
    public List<String> getNames(List<HousePayWayBean> choiceRoles) {
        if (!mBuilder.isSetData) {
            mBuilder.firstNames.clear();
            if (choiceRoles != null && !choiceRoles.isEmpty()) {
                for (HousePayWayBean housePayWayBean : choiceRoles) {
                    mBuilder.firstNames.add(housePayWayBean.getName());
                }
            }
        }
        return mBuilder.firstNames;
    }

    /**
     * init listener
     */
    private void initListener() {
        this.mOnConfirmListener = mBuilder.mOnConfirmListener;
        mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });

        confirmBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MessageEvent messageEvent = null;
                if (mBuilder.ishx) {
                    messageEvent = new MessageEvent(mBuilder.firstNames.get(loopView.getSelectedItem()) + mBuilder.secondNames.get(loopView1.getSelectedItem()) + mBuilder.thirdNames.get(loopView2.getSelectedItem()),
                            loopView.getSelectedItem(), loopView1.getSelectedItem(), loopView2.getSelectedItem());
                    messageEvent.setRoom(mBuilder.firstNames.get(loopView.getSelectedItem()));
                    messageEvent.setDrawing(mBuilder.secondNames.get(loopView1.getSelectedItem()));
                    messageEvent.setToilet(mBuilder.thirdNames.get(loopView2.getSelectedItem()));
                    mOnConfirmListener.onClick(messageEvent);
                    dismiss();
                    return;
                }
                if (mBuilder.secondNames==null||mBuilder.secondNames.isEmpty()) {
                    //没有第二级,只有第一级
                    if (mBuilder.firstNames == null||mBuilder.firstNames.isEmpty()) {
                        messageEvent = new MessageEvent( "", 0);
                        mOnConfirmListener.onClick(messageEvent);
                        dismiss();
                        return;
                    }
                    messageEvent = new MessageEvent(!mBuilder.firstNames.isEmpty() ? mBuilder.firstNames.get(loopView.getSelectedItem()) : "", loopView.getSelectedItem());
                    if (mBuilder.firstNames != null && !mBuilder.firstNames.isEmpty()) {
                        messageEvent.setRoom(mBuilder.firstNames.get(loopView.getSelectedItem()));
                        messageEvent.setRoomIndex(loopView.getSelectedItem());
                    }
                    if (mBuilder.secondNames != null && !mBuilder.secondNames.isEmpty()) {
                        messageEvent.setDrawing(mBuilder.secondNames.get(loopView1.getSelectedItem()));
                        messageEvent.setDrawingIndex(loopView1.getSelectedItem());
                    }
                    if (mBuilder.thirdNames != null && !mBuilder.thirdNames.isEmpty()) {
                        messageEvent.setToilet(mBuilder.thirdNames.get(loopView2.getSelectedItem()));
                        messageEvent.setToiletIndex(loopView2.getSelectedItem());
                    }
                    if (mBuilder.choiceRoles!=null&&mBuilder.choiceRoles.size() > loopView.getSelectedItem()) {
                        messageEvent.setLat(mBuilder.choiceRoles.get(loopView.getSelectedItem()).getLat());
                        messageEvent.setLng(mBuilder.choiceRoles.get(loopView.getSelectedItem()).getLgt());
                    }
                } else if (mBuilder.thirdNames!=null&&mBuilder.thirdNames.isEmpty()) {
                    //没有第三级
                    messageEvent = new MessageEvent(!mBuilder.secondNames.isEmpty() ? mBuilder.secondNames.get(loopView1.getSelectedItem()) : "", loopView1.getSelectedItem());
                    if (mBuilder.firstNames != null && !mBuilder.firstNames.isEmpty()) {
                        messageEvent.setRoom(mBuilder.firstNames.get(loopView.getSelectedItem()));
                        messageEvent.setRoomIndex(loopView.getSelectedItem());
                    }
                    if (mBuilder.secondNames != null && !mBuilder.secondNames.isEmpty()) {
                        messageEvent.setDrawing(mBuilder.secondNames.get(loopView1.getSelectedItem()));
                        messageEvent.setDrawingIndex(loopView1.getSelectedItem());
                    }
                    if (mBuilder.thirdNames != null && !mBuilder.thirdNames.isEmpty()) {
                        messageEvent.setToilet(mBuilder.thirdNames.get(loopView2.getSelectedItem()));
                        messageEvent.setToiletIndex(loopView2.getSelectedItem());
                    }
                    if (mBuilder.choiceRoles != null && mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().size() > loopView1.getSelectedItem()) {
                        messageEvent = new MessageEvent(mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().get(loopView1.getSelectedItem()).getName(),loopView1.getSelectedItem());
                        messageEvent.setRoomIndex(loopView.getSelectedItem());
                        messageEvent.setRoom(mBuilder.choiceRoles.get(loopView.getSelectedItem()).getName());
                        messageEvent.setDrawingIndex(loopView1.getSelectedItem());
                        messageEvent.setDrawing(mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().get(loopView1.getSelectedItem()).getName());
                        messageEvent.setLat(mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().get(loopView1.getSelectedItem()).getLat());
                        messageEvent.setLng(mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().get(loopView1.getSelectedItem()).getLgt());
                    }
                } else {
                    messageEvent = new MessageEvent(!mBuilder.thirdNames.isEmpty() ? mBuilder.thirdNames.get(loopView2.getSelectedItem()) : "", loopView2.getSelectedItem());
                    if (mBuilder.firstNames != null && !mBuilder.firstNames.isEmpty()) {
                        messageEvent.setRoom(mBuilder.firstNames.get(loopView.getSelectedItem()));
                        messageEvent.setRoomIndex(loopView.getSelectedItem());
                    }
                    if (mBuilder.secondNames != null && !mBuilder.secondNames.isEmpty()) {
                        messageEvent.setDrawing(mBuilder.secondNames.get(loopView1.getSelectedItem()));
                        messageEvent.setDrawingIndex(loopView1.getSelectedItem());
                    }
                    if (mBuilder.thirdNames != null && !mBuilder.thirdNames.isEmpty() && mBuilder.thirdNames.size() > loopView2.getSelectedItem()) {
                        messageEvent.setToilet(mBuilder.thirdNames.get(loopView2.getSelectedItem()));
                        messageEvent.setToiletIndex(loopView2.getSelectedItem());
                    }
                    if (mBuilder.choiceRoles != null && mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().get(loopView1.getSelectedItem()).getChildren().size() > loopView2.getSelectedItem()) {
                        messageEvent.setLat(mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().get(loopView1.getSelectedItem()).getChildren().get(loopView2.getSelectedItem()).getLat());
                        messageEvent.setLng(mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().get(loopView1.getSelectedItem()).getChildren().get(loopView2.getSelectedItem()).getLgt());
                    }
                }
                mOnConfirmListener.onClick(messageEvent);
                dismiss();
            }
        });
        loopView.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if (mBuilder.choiceRoles != null && !mBuilder.choiceRoles.isEmpty()) {
                    getSecondNames(mBuilder.choiceRoles.get(index).getChildren());
                }
                if (mBuilder.secondNames!=null&&!mBuilder.secondNames.isEmpty()) {
                    loopView1.setVisibility(View.VISIBLE);
                    showloopView1(mBuilder.secondNames, 0);
                    if (mBuilder.choiceRoles != null && mBuilder.choiceRoles.get(index).getChildren() != null && !mBuilder.choiceRoles.get(index).getChildren().isEmpty()) {
                        getThirdNames(mBuilder.choiceRoles.get(index).getChildren().get(0).getChildren());
                        if (!mBuilder.thirdNames.isEmpty()) {
                            loopView2.setVisibility(View.VISIBLE);
                            showloopView2(mBuilder.thirdNames, 0);
                        } else {
                            loopView2.setVisibility(View.GONE);

                        }
                    }
                } else {
                    loopView1.setVisibility(View.GONE);

                }
            }
        });
        loopView1.setListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                if (mBuilder.choiceRoles != null && !mBuilder.choiceRoles.isEmpty() && mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren() != null && !mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().isEmpty() && mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().get(index) != null && !mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().get(index).getChildren().isEmpty()) {
                    getThirdNames(mBuilder.choiceRoles.get(loopView.getSelectedItem()).getChildren().get(index).getChildren());
                }
                if (!mBuilder.thirdNames.isEmpty()) {
                    loopView2.setVisibility(View.VISIBLE);
                    showloopView2(mBuilder.thirdNames, 0);
                } else {
                    loopView2.setVisibility(View.GONE);

                }
            }
        });
        if (mBuilder.mOnCancelListener != null) {
            cancelBtn.setOnClickListener(mBuilder.mOnCancelListener);
        }

        cancelBtn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                dismiss();
                return false;
            }
        });
    }

    private void initViews(View root) {
        titleTV = (TextView) root.findViewById(R.id.title);
        cancelBtn = (TextView) root.findViewById(R.id.cancelBtn);
        confirmBtn = (TextView) root.findViewById(R.id.confirmBtn);
        setText(titleTV, mBuilder.title);
        setText(cancelBtn, mBuilder.cancelText);
        setText(confirmBtn, mBuilder.confirmText);

        setTextColor(titleTV, mBuilder.mTitleTextColor);
        setTextColor(cancelBtn, mBuilder.mCancelTextColor);
        setTextColor(confirmBtn, mBuilder.mConfirmTextColor);
        loopView = (LoopView) root.findViewById(R.id.type_loopview);
        loopView1 = (LoopView) root.findViewById(R.id.type_loopview1);
        loopView2 = (LoopView) root.findViewById(R.id.type_loopview2);

    }

    /**
     * 滚动选择
     */
    public void showloopView2(List<String> list, int defultIndex) {
        if (list != null && !list.isEmpty()) {
            loopView2.setVisibility(View.VISIBLE);
            loopView2.setNotLoop();
            loopView2.setItems(list);
            loopView2.setInitPosition(defultIndex);
        }else {
            loopView2.setVisibility(View.GONE);
        }
    }

    /**
     * 滚动选择
     */
    public void showloopView1(List<String> list, int defultIndex) {
        if (list != null && !list.isEmpty()) {
            loopView1.setVisibility(View.VISIBLE);
            loopView1.setNotLoop();
            loopView1.setItems(list);
            loopView1.setInitPosition(defultIndex);
        }else {
            loopView1.setVisibility(View.GONE);
        }

    }

    /**
     * 滚动选择
     */
    public void showloopView(List<String> list, int defultIndex) {
        if (list != null && !list.isEmpty()) {
            loopView.setVisibility(View.VISIBLE);
            loopView.setNotLoop();
            loopView.setItems(list);
            loopView.setInitPosition(defultIndex);
        }else {
            loopView.setVisibility(View.GONE);
        }
    }

    private void setText(TextView tv, String str) {
        if (tv != null && str != null) {
            tv.setText(str);
        }
    }

    private void setTextColor(TextView tv, int color) {
        if (tv != null && color != 0) {
            tv.setTextColor(color);
        }
    }

    public void dismiss() {
        if (mPopupWindow != null) {
            mPopupWindow.dismiss();
        }
    }

    /**
     * parent is the popwindow show location
     *
     * @param parent
     */
    public void show(View parent) {
        if (mPopupWindow != null) {
            backgroundAlpha(0.4f);
            mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        }
    }

    /**
     * set background alpha
     *
     * @param alpha
     */
    public void backgroundAlpha(float alpha) {
        try {
            WindowManager.LayoutParams lp = mBuilder.mActivity.getWindow().getAttributes();
            lp.alpha = alpha; //0.0-1.0
            mBuilder.mActivity.getWindow().setAttributes(lp);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
